package com.vip.web.controller;



import lombok.extern.slf4j.Slf4j;
import com.vip.dao.action.domain.SysOperLog;
import com.vip.dao.admin.domain.User;
import com.vip.service.action.SysOperLogService;
import com.vip.service.admin.UserService;
import com.vip.web.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@Slf4j
@Api(value = "登录模块接口")
@RestController
public class LoginController {

    @Autowired
    UserService userService;

    @Autowired
    SysOperLogService operLogService;


    /**
     * 登录，并记录登录日志
     * @param userName
     * @param password
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "用户名，密码登录")
    @RequestMapping(value = "/sys/login/{userName}/{password}",method = RequestMethod.GET)
    private Result login(@PathVariable("userName") String userName, @PathVariable("password") String password){
        SysOperLog operLog = new SysOperLog();
        operLog.setTitle("登录模块");
        operLog.setMethod("POST");
        operLog.setOperatorType(1);
        operLog.setOperName(userName);
        operLog.setOperUrl("sys/login");
        operLog.setOperIp("127.0.0.1");
        User user = userService.findUserByUserNameAndPassword(userName,password);
        log.info("user ===> " + user.toString());
        if(null != user){
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","登录成功");
            map.put("status",200);
            operLog.setStatus(1);
            operLogService.addOneOperLog(operLog);
            return  Result.success(user,map);
        }else{
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","登录失败");
            map.put("status",400);
            operLog.setStatus(0);
            operLogService.addOneOperLog(operLog);
            return  Result.success(null,map);
        }
    }

    @ResponseBody
    @ApiOperation(value = "获取所有日志")
    @RequestMapping(value = "/sys/findMyOperLog",method = RequestMethod.GET)
    public Result findUserOrder(){
        List<SysOperLog> logs = operLogService.findAllLog();
        if(null != logs){
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","获取日志列表成功");
            map.put("status",200);
            return Result.success(logs,map);
        }else{
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","日志列表为空");
            map.put("status",250);
            return Result.success(null,map);
        }

    }

    @ResponseBody
    @ApiOperation(value = "根据日志id删除日志")
    @RequestMapping(value = "/sys/delOperLogById/{id}",method = RequestMethod.GET)
    public Result deleLogById(@PathVariable("id") Integer id){
            operLogService.deleteById(id.longValue());
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","删除日志成功");
            map.put("status",200);
            return Result.success(null,map);


    }

}
